package com.gitee.swsk33.lucenedemo;

import cn.hutool.core.io.FileUtil;
import com.gitee.swsk33.lucenedemo.model.MarkdownFile;
import com.gitee.swsk33.lucenedemo.model.SearchInput;
import com.gitee.swsk33.lucenedemo.model.SearchResult;

import java.io.File;
import java.nio.file.Paths;
import java.util.List;

public class Main {

	private static final String INDEX_DIR = "index";

	private static LuceneIndexOperator OPERATOR;

	static {
		try {
			OPERATOR = new LuceneIndexOperator(INDEX_DIR);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 创建索引
	 */
	private static void createIndex() throws Exception {
		// 读取本地文档到索引
		File[] files = FileUtil.ls(Paths.get("docs").toAbsolutePath().toString());
		int id = 1;
		for (File file : files) {
			OPERATOR.addOrSetDocument(MarkdownFile.create(String.valueOf(id++), file.getAbsolutePath()));
		}
	}

	public static void main(String[] args) throws Exception {
		SearchInput input = new SearchInput("content", "我", 1, 1);
		System.out.println("查询到：" + OPERATOR.searchDocumentCount(input) + "条记录");
		List<SearchResult> results = OPERATOR.searchDocument(input);
		for (SearchResult result : results) {
			System.out.println(result);
		}
		OPERATOR.close();
	}

}